<?php
/**
 * Vhost
 * @author Tomas Srna
 * @package bakalarka
 */

class Vhost
{
	/**
	 * @var HostingModel $hosting	Objekt hostingu
	 */
	protected $hosting = NULL;
	
	protected $isSub, $isSSL;
	
	public function __construct($hosting, $isSSL = false, $isSub = false)
	{
		if(!defined('SSL_INCLUDE'))
			exit('SSL_INCLUDE must be defined.');
		if(!defined('OPEN_BASEDIR_DEFAULT'))
			exit('OPEN_BASEDIR_DEFAULT must be defined.');
		if(!defined('TEMP_DIR'))
			exit('TEMP_DIR must be defined.');
		$this->hosting = $hosting;
		$this->isSSL = $isSSL;
		$this->isSub = $isSub;
	}
	
	public function __toString()
	{
		$si = ($this->isSSL?"\t":"");
		
		$r = "";
		
		if($this->isSSL)
			$r .= "<IfModule mod_ssl.c>\n";
		
		$r .= "$si<VirtualHost *:".($this->isSSL?443:80).">\n";
		
		if($this->hosting->servername)
			$r.= "$si\tServerName {$this->hosting->servername}\n";
		if($this->hosting->serveralias)
			$r.= "$si\tServerAlias {$this->hosting->serveralias}\n";
		if($this->hosting->adminmail)
			$r.= "$si\tServerAdmin {$this->hosting->adminmail}\n";
		
		// DocumentRoot
		if(!$this->isSub)
		{
			if($this->hosting->root && $this->hosting->documentroot)
				$r.= "$si\tDocumentRoot /".trim($this->hosting->root, "/").
						"/".trim($this->hosting->documentroot, "/")."\n";
		}
		// VirtualDocumentRoot
		else
		{
			$sdstring = "%-".(substr_count($this->hosting->servername,
				 ".")+2).".0";
				 
			$droot = "/".trim($this->hosting->root, "/").
						"/".@sprintf(trim($this->hosting->subdomaindir, "/"),
							$sdstring);
			
			$r.= "$si\tVirtualDocumentRoot $droot\n";
		}
		
		if($this->hosting->php)
			$r .= "$si\tphp_flag engine On\n";
		else
			$r .= "$si\tphp_flag engine Off\n";
			
		// open_basedir
		$open_basedir = "";
		if(!$this->hosting->openbasedir || 
			substr($this->hosting->openbasedir, 0, 1) == ':')
			$open_basedir .= OPEN_BASEDIR_DEFAULT;
		$open_basedir .= $this->hosting->openbasedir;
		$open_basedir = str_replace('%Name%', trim($this->hosting->name, '/'),
							$open_basedir);
		$open_basedir = str_replace('%Root%', trim($this->hosting->root, '/'),
							$open_basedir);
		$open_basedir = str_replace('%DocumentRoot%', trim(
							$this->hosting->documentroot,'/'), $open_basedir);
		$r .= "$si\tphp_admin_value open_basedir $open_basedir\n";
		
		$temp_dir = str_replace('%Name%', trim($this->hosting->name, '/'),
							TEMP_DIR);
		
		$r .= "$si\tphp_admin_value upload_tmp_dir $temp_dir\n";
		$r .= "$si\tphp_admin_value session.save_path $temp_dir\n";
		$r .= "$si\tphp_admin_value soap.wsdl_cache_dir $temp_dir\n";
		
		// Directory
		$r.= "$si\t<Directory /".trim($this->hosting->root, "/").
					"/".trim($this->hosting->documentroot, "/").">\n";
		$r.= "$si\t\tOptions ".($this->hosting->indexes?"":"-")."Indexes".
				" ".($this->hosting->symlinks?"":"-")."FollowSymLinks".
				"\n";
		$r.= "$si\t\tAllowOverride ".($this->hosting->allowoverride?"All":"None").
				"\n";
				
		$r.= "$si\t\tOrder allow,deny\n$si\t\tAllow from all\n";
		$r.= "$si\t</Directory>\n";
		
		if($this->isSSL)
			$r .= "$si\tInclude ".SSL_INCLUDE."\n";
			
		// Logy
		$logdir = "/".trim($this->hosting->root, "/")."/logs";
		$r.= "$si\tErrorLog $logdir/error.log\n";
		$r.= "$si\tLogLevel warn\n";
		$r.= "$si\tCustomLog $logdir/access.log combined\n";
		
		$r .= "$si</VirtualHost>\n";
		
		if($this->isSSL)
			$r .= "</IfModule>\n";
		
		return $r;
	}
}
